package com.hz.filemanager.ui.file.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.hz.filemanager.R;
import com.hz.filemanager.expendable_recyclerview.BaseCheckableExpandableRecyclerViewAdapter;
import com.hz.filemanager.model.file.ImageBean;
import com.hz.filemanager.model.file.ImgFolderBean;

import java.util.List;

public class ImageCheckableAdapter extends BaseCheckableExpandableRecyclerViewAdapter<ImgFolderBean, ImageBean, ImageCheckableAdapter.GroupVH,ImageCheckableAdapter.ChildVH> {

    private Context context;
    private List<ImgFolderBean> datas;

    public List<ImgFolderBean> getDatas() {
        return datas;
    }

    public void setDatas(List<ImgFolderBean> datas) {
        this.datas = datas;
    }

    public ImageCheckableAdapter(int maxCheckedNum) {
        super(maxCheckedNum);
    }

    public ImageCheckableAdapter(int maxCheckedNum, Context context) {
        super(maxCheckedNum);
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return datas.size();
    }

    @Override
    public ImgFolderBean getGroupItem(int groupIndex) {
        return datas.get(groupIndex);
    }

    @Override
    public GroupVH onCreateGroupViewHolder(ViewGroup parent, int groupViewType) {
        return new GroupVH(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.listitem_group, parent, false));
    }

    @Override
    protected void onBindGroupViewHolder(GroupVH holder, ImgFolderBean groupBean, boolean isExpand, List<Object> payload,int position) {
        holder.nameTv.setText(groupBean.getName());
        holder.groupSizeTv.setText("("+groupBean.getChildCount()+")");
        if (groupBean.isExpandable()) {
            holder.foldIv.setVisibility(View.VISIBLE);
            holder.foldIv.setImageResource(isExpand ? R.mipmap.icon_expand : R.mipmap.icon_no_expand);
        } else {
            holder.foldIv.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public ChildVH onCreateChildViewHolder(ViewGroup parent, int childViewType) {
        return new ChildVH(
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.listitem_child, parent, false));
    }

    @Override
    protected void onBindChildViewHolder(ChildVH holder, ImgFolderBean imgFolderBean, ImageBean imageBean, List<Object> payload,int position) {
        Glide.with(context)
                .load(imageBean.getPath())
                .transform(new CenterCrop(),new RoundedCorners(25))
                .placeholder(R.color.cbcbcb)
                .error(R.color.cbcbcb)
                .into(holder.iv);
    }

    static class GroupVH extends BaseCheckableExpandableRecyclerViewAdapter.BaseCheckableGroupViewHolder<ImgFolderBean>{

        TextView groupSizeTv;
        ImageView foldIv;
        TextView nameTv;

        public GroupVH(View itemView) {
            super(itemView);
            foldIv = (ImageView) itemView.findViewById(R.id.group_item_indicator);
            nameTv = (TextView) itemView.findViewById(R.id.group_item_name);
            groupSizeTv = itemView.findViewById(R.id.group_size);
        }

        @Override
        protected void onExpandStatusChanged(RecyclerView.Adapter relatedAdapter, boolean isExpanding) {
            foldIv.setImageResource(isExpanding ? R.mipmap.icon_expand : R.mipmap.icon_no_expand);
        }

        @Override
        public void setCheckMode(int mode, ImgFolderBean childBean) {

        }

        @Override
        public View getCheckableRegion() {
            return null;
        }


    }

    static class ChildVH extends BaseCheckableExpandableRecyclerViewAdapter.BaseCheckableChildViewHolder<ImageBean>{

        ImageView iv;

        public ChildVH(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.child_iv);
        }

        @Override
        public void setCheckMode(int mode, ImageBean childBean) {

        }

        @Override
        public View getCheckableRegion() {
            return null;
        }
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if(layoutManager instanceof GridLayoutManager){
            final GridLayoutManager gridManager = ((GridLayoutManager) layoutManager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if (getItemViewType(position) == TYPE_GROUP) {
                        return 3; //返回3展示一行
                    } else {
                        return 1;//返回1展示3列
                    }

                }
            });
        }
    }
}
